package com.bifrost.service.impl;

import com.bifrost.cbentity.SapContract;
import com.bifrost.cbentity.SapContractProduct;
import com.bifrost.cbrepository.SapContractRepository;
import com.bifrost.service.SapContractService;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Lee
 * Create Class on 2018/7/26
 * @com.bifrost.service
 * @wechat-mall
 */
@Service
public class SapContractServiceImpl implements SapContractService{

    @Autowired
    SapContractRepository contractRepository;

    @Override
    public void verifyContract(SapContractProduct contractProduct , @Nullable Date activeEndDate) {
        Boolean resultFlag = false ;
        if(activeEndDate==null){
            activeEndDate = new Date();
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String stringDate = format.format(activeEndDate);

        //根据柜号 找到多个合同号
        List<SapContract> contractList = contractRepository.findAllByZzzzghnAndBenocn(contractProduct.getZzghn(), contractProduct.getWerks(), stringDate);
        if (!contractList.isEmpty()) {
            // 得到生效合同号
            resultFlag = contractList.stream().filter(sapContract -> {
                return sapContract.getZzcontStatus().equalsIgnoreCase("生效") || sapContract.getZzcontStatus().equalsIgnoreCase("续签");
            }).count() > 0;
        }
        Assert.isTrue(resultFlag,"活动日期不在合同有效期内");
    }
}
